﻿// -----------------------------------------------------------------------
// <copyright file="Constants.cs" company="Meligo">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Meligo.ModuleExtractor.GenerateModuleFactory
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class Constants
    {
        private const string EmptyListViewWebPart = @"<webParts>
                        <webPart xmlns='http://schemas.microsoft.com/WebPart/v3'>
                            <metaData>
                                <type name='Microsoft.SharePoint.WebPartPages.XsltListViewWebPart,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c' />
                                <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
                            </metaData>
                            <data>
                                <properties>
                                    <property name='ChromeType' type='chrometype'>TitleOnly</property>
                                    <property name='Title' type='string'>{0}</property>
                                    <property name='Hidden' type='bool'>{1}</property>
                                </properties>
                            </data>
                        </webPart>
                    </webParts>";

        public static string GetListViewWebpart(string propertiesXml, string listname)
        {
            XNamespace ns = "http://schemas.microsoft.com/WebPart/v3";
            XDocument exportStringDoc = XDocument.Parse(propertiesXml, LoadOptions.None);
            IEnumerable<XElement> lijst = from node in exportStringDoc.Root.Descendants(ns + "property")
                                          select node;
            StringBuilder builder = new StringBuilder();
            string hiddenstring = string.Empty;
            string titleName = string.Empty;
            foreach (XElement elements in lijst)
            {
                switch (elements.Attribute("name").Value)
                {
                    case "Title":
                        titleName = elements.Value.ToString();
                        break;
                    case "Hidden":
                        hiddenstring = elements.Value.ToString();
                        break;
                    default:
                        break;
                }
            }

            if (!string.IsNullOrEmpty(titleName))
            {
                return string.Format(EmptyListViewWebPart, titleName, hiddenstring);
            }
            else
            {
                return string.Format(EmptyListViewWebPart, listname, hiddenstring);
            }
        }
    }
}
